package com.raindrop.rd;

import com.raindrop.rd.parser.ast.Block;

import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.List;

/**
 * RD语言执行器，代表每个文件编译后的代码，传入上下文可以直接执行
 *
 * @author BillDowney
 * @date 2021/3/22 15:46
 */
public class RDCompiledScript extends CompiledScript {
    /**
     * 语言引擎
     */
    private final RDScriptEngine engine;
    /**
     * 编译后的代码块
     */
    private final List<Block> blocks;

    public RDCompiledScript(RDScriptEngine engine, List<Block> blocks) {
        this.engine = engine;
        this.blocks = blocks;
    }

    @Override
    public Object eval(ScriptContext context) throws ScriptException {
        Object returnValue = Void.class;
        for (Block block : blocks) {
            System.out.println(block.eval(context));
        }
        return returnValue;
    }

    @Override
    public ScriptEngine getEngine() {
        return engine;
    }
}
